#define _CRT_SECURE_NO_WARNINGS
#include"Queue.h"

void test1()
{
	Queue que;
	QueueInit(&que);
	QueuePush(&que, 1);
	QueuePush(&que, 2);
	QueuePush(&que, 3);
	QueuePush(&que, 4);
	QueuePush(&que, 5);
	printf("%d\n", QueueSize(&que));
	//QueueDestroy(&que);
	while (!QueueEmpty(&que))
	{
		printf("%d ", QueueFront(&que));
		QueuePop(&que);

	}

	QueueDestroy(&que);

}
int main()
{
	test1();
	return 0;
}


typedef int QueDataType;

typedef struct QueueNode
{
    QueDataType data;
    struct QueueNode* next;
}QueNode;

typedef struct Queue
{
    QueNode* head;
    QueNode* tail;
    int size;
}Queue;

typedef struct {
    Queue q1;
    Queue q2;
} MyStack;


MyStack* myStackCreate() {
    MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
    if (obj == NULL)
    {
        perror("malloc fail");
        return NULL;
    }

    QueueInit(&obj->q1);
    QueueInit(&obj->q2);
    return obj;
}

void myStackPush(MyStack* obj, int x) {
    if (QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1, x);
    }
    else {
        QueuePush(&obj->q2, x);
    }
}

int myStackPop(MyStack* obj) {
    Queue* empty = &obj->q1;
    Queue* nonempty = &obj->q2;
    if (QueueEmpty(&obj->q2))
    {
        empty = &obj->q2;
        nonempty = &obj->q1;
    }

    while (QueueSize(nonempty) > 1)
    {
        QueuePush(empty, QueueFront(nonempty));
        QueuePop(nonempty);
    }
    int top = QueueFront(nonempty);
    QueuePop(nonempty);
    return top;
}

int myStackTop(MyStack* obj) {
    Queue* empty = &obj->q1;
    Queue* nonempty = &obj->q2;
    if (QueueEmpty(&obj->q2))
    {
        empty = &obj->q2;
        nonempty = &obj->q1;
    }
    return QueueBack(nonempty);
}

bool myStackEmpty(MyStack* obj) {

    return (QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2));

}

void myStackFree(MyStack* obj) {
    QueueDestroy(&obj->q1);
    QueueDestroy(&obj->q2);

    free(obj);

}